<template>
  <div class="home-container">
    <!-- 轮播图区域 -->
    <div class="banner-carousel">
      <el-carousel :interval="0" height="400px">
        <el-carousel-item v-for="item in carouselList" :key="item.id">
          <el-image
            :src="item.imageUrl"
            fit="cover"
            class="carousel-image"
            :preview-src-list="[item.imageUrl]"
          />
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 系统介绍区域 -->
    <div class="system-intro">
      <h2 class="section-title">欢迎来到昆虫爱好者交流平台</h2>
      <div class="intro-content">
        <div class="feature-card">
          <el-icon class="feature-icon"><UserFilled /></el-icon>
          <h4>专业社区</h4>
          <p>加入全球昆虫爱好者的专业交流社区，分享观察记录和研究成果</p>
        </div>
        <div class="feature-card">
          <el-icon class="feature-icon"><Files /></el-icon>
          <h4>知识库</h4>
          <p>访问专业昆虫图鉴和分类资料，获取权威的物种鉴定信息</p>
        </div>
        <div class="feature-card">
          <el-icon class="feature-icon"><Position /></el-icon>
          <h4>地图定位</h4>
          <p>查看全球昆虫分布热力图，记录和分享你的观察位置</p>
        </div>
      </div>
    </div>

    <!-- 平台特色区域 -->
    <div class="platform-features">
      <div class="feature-item">
        <el-icon class="feature-icon-large"><CollectionTag /></el-icon>
        <div class="feature-text">
          <h4>标本交易</h4>
          <p>安全可靠的标本交易平台，支持专业级包装和运输</p>
        </div>
      </div>
      <div class="feature-item">
        <el-icon class="feature-icon-large"><ChatDotRound /></el-icon>
        <div class="feature-text">
          <h4>专家咨询</h4>
          <p>与昆虫学专家在线交流，获取专业指导和建议</p>
        </div>
      </div>
      <div class="feature-item">
        <el-icon class="feature-icon-large"><DataAnalysis /></el-icon>
        <div class="feature-text">
          <h4>数据分析</h4>
          <p>提供物种分布统计和观察趋势分析报告</p>
        </div>
      </div>
      <div class="feature-item">
        <el-icon class="feature-icon-large"><School /></el-icon>
        <div class="feature-text">
          <h4>在线课程</h4>
          <p>提供昆虫分类学、生态摄影等专业课程</p>
        </div>
      </div>
    </div>

    <!-- 公告展示区域 -->
    <div class="announcement-section">
      <h2 class="section-title">系统公告</h2>
      <div class="announcement-list">
        <div
          v-for="item in announcementList"
          :key="item.id"
          class="announcement-item"
          @click="viewAnnouncementDetail(item.id)"
        >
          <h4>{{ item.title }}</h4>
        </div>
      </div>
    </div>

    <!-- 公告详情弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title="公告详情"
      width="50%"
      :before-close="handleClose"
    >
      <div v-html="selectedAnnouncement.content" class="announcement-detail"></div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
// 导入 onMounted 方法
import { ref, onMounted } from 'vue'
import userApi from '../apis/UserApi.js'
import { UserFilled, Files, Position, CollectionTag, ChatDotRound, DataAnalysis, School } from '@element-plus/icons-vue'

const carouselList = ref([])
const announcementList = ref([])

onMounted(async () => {
  try {
    const res = await userApi.getAllCarousels({
      currentPage: 1,
      pageSize: 5,
      status: 1 // 只获取启用的轮播图
    })
    if (res.code === 200) {
      carouselList.value = res.data.records
    }
  } catch (error) {
    console.error('轮播图加载失败:', error)
  }

  // 获取所有公告列表数据
  try {
    const res = await userApi.getAnnouncementList({
      currentPage: 1,
      pageSize: 10, // 增加页面大小以获取更多公告
    })
    if (res.code === 200) {
      announcementList.value = res.data.records.sort((a, b) => new Date(b.createTime) - new Date(a.createTime)) // 按创建时间排序，最新的在最上面
    }
  } catch (error) {
    console.error('公告加载失败:', error)
  }
})

// 格式化日期方法
const formatDate = (dateStr: string) => {
  const date = new Date(dateStr)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
}

// 控制弹窗显示与当前选中公告
const dialogVisible = ref(false)
const selectedAnnouncement = ref({ id: '', title: '', content: '' })

// 查看公告详情
const viewAnnouncementDetail = async (id: number) => {
  try {
    const res = await userApi.getAnnouncementDetail(id)
    if (res.code === 200) {
      selectedAnnouncement.value = res.data
      dialogVisible.value = true
    }
  } catch (error) {
    console.error('获取公告详情失败:', error)
  }
}

// 关闭弹窗前的处理
const handleClose = () => {
  dialogVisible.value = false
}
</script>

<style scoped>
.home-container {
  padding: 20px;
  max-width: 1440px;
  margin: 0 auto;
}

.banner-carousel {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 40px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.system-intro {
  margin-bottom: 60px;
  padding: 0 20px;
}

.section-title {
  font-size: 28px;
  color: #2c3e50;
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.section-title::after {
  content: '';
  display: block;
  width: 60px;
  height: 3px;
  background: #409EFF;
  margin: 15px auto 0;
}

.intro-content {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 30px;
}

.feature-card {
  background: #fff;
  padding: 25px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-icon {
  font-size: 42px;
  color: #409EFF;
  margin-bottom: 15px;
}

.feature-card h4 {
  font-size: 18px;
  color: #333;
  margin: 15px 0;
}

.feature-card p {
  color: #666;
  line-height: 1.6;
}

.platform-features {
  background: #f5f7fa;
  padding: 40px 20px;
  border-radius: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 30px;
}

.feature-item {
  display: flex;
  align-items: center;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.feature-item:hover {
  transform: translateX(10px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.feature-icon-large {
  font-size: 56px;
  color: #409EFF;
  margin-right: 20px;
  flex-shrink: 0;
}

.feature-text h4 {
  font-size: 20px;
  color: #333;
  margin-bottom: 8px;
}

.feature-text p {
  color: #777;
  line-height: 1.5;
}

@media (max-width: 1024px) {
  .intro-content {
    grid-template-columns: repeat(2, 1fr);
  }

  .platform-features {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .intro-content {
    grid-template-columns: 1fr;
  }

  .platform-features {
    grid-template-columns: 1fr;
  }

  .feature-item {
    flex-direction: column;
    text-align: center;
  }

  .feature-icon-large {
    margin-bottom: 15px;
  }
}

/* 公告展示区域样式 */
.announcement-section {
  margin-bottom: 60px;
  padding: 0 20px;
}

.announcement-list {
  display: grid;
  gap: 20px;
}

.announcement-item {
  background: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease;
}

.announcement-item:hover {
  transform: translateY(-5px);
}

/* 公告标题样式优化 */
.announcement-item h4 {
  font-size: 18px;
  color: #409EFF;
  cursor: pointer;
  margin-bottom: 10px;
  transition: color 0.3s ease;
}

.announcement-item h4:hover {
  color: #66b1ff;
}

/* 弹窗内容样式 */
.announcement-detail {
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  padding: 10px;
  border-radius: 4px;
  background: #f9f9f9;
}
</style>
